import os

from flask import Flask, request, session

app = Flask(__name__, template_folder='template')

# 启用session
app.config['SECRET_KEY'] = os.urandom(24) # 生成随机数种子，用于生成sessionId

from controller.login import l
app.register_blueprint(l)

# 请求拦截器
@app.before_request
def before_request():
    ### 获取url，如果是/login则放过
    url = request.path
    print(url)
    # 定义白名单
    pass_path = ['/', '/login', '/reg']
    # 定义后缀
    suffix = url.endswith('png') or url.endswith('jpg') or url.endswith('jpeg')
    print(suffix)

    if url in pass_path or suffix:
        pass
    else:
        is_login = session.get('is_login')
        if is_login != True:
            return "请登录"


# 错误处理
@app.errorhandler(404)
def page_not_found(e):
    print(e)
    return "输入的网址错误"

@app.errorhandler(500)
def internal_server_error(e):
    print(e)
    return "服务器内部错误"

if __name__ == '__main__':
    app.run()